// pages/my/my.js
import CommonPage from "../common/CommonPage"
const app = getApp()
const api = require('../api/http.js')
const config = require('../../config.js')

class MyPage extends CommonPage {

  /**
   * 页面的初始数据
   */
  constructor(...args) {
    super(...args)
    super.$init({
      name: '',
      height: '128rpx',
      hd: '../../images/plus.png',
      openId: '',
      titleName: "我的"
    })
  }
  initData = function(query, resolve, reject) {
    this.data.height = app.globalData.titleHeight
    var user = wx.getStorageSync('userInfo')
    if (user.openId) {
      this.data.name = user.nickName
      this.data.hd = user.avatarUrl
      this.data.openId = user.openId
      console.debug("on MyPage本地缓存用户信息：", this.data)
    } else if (app.globalData.hasUserInfo) {
      this.data.name = app.globalData.userInfo.nickName
      this.data.hd = app.globalData.userInfo.avatarUrl
      this.data.openId = app.globalData.userInfo.openId
      console.debug("on MyPage全局信息：", this.data)
    } else {
      console.error("未获取用户信息！")
    }
    this.$setData(this.data)
  }

  $onNavigator(query) {
    this.$put("my-data", this.initData.bind(this), query)
  }
  clickQxgl(params) {
    wx.openSetting();
  }
  copy = function() {
    var that = this
    wx.setClipboardData({
      data: that.data.openId,
    })
  }
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad = function(query) {
    var dat = this.$take("my-data")
    if (dat) return 
    this.initData(query)
  }
  onShow = function() {
    if (!this.data.openId) {
      this.initData()
    }
  }

  //事件处理函数
  bindViewTap = function() {
    wx.navigateTo({
      url: '../../logs/logs'
    })
  }
  toRuler = function() {
    this.$route({path: "./ruler/ruler", query: {}, clazzName: "RulerPage"})
  }
  toDaoCoin = function() {
    if (app.globalData.hasUserInfo) {
      wx.navigateTo({
        url: './daoCoin/daoCoin',
      })
    } else {
      wx.showToast({
        title: '请先授权登录^_^',
        icon: 'none',
      })
    }
  }
  toSuit = function() {
    var user = wx.getStorageSync('userInfo')

    if (user.openId) {
      this.$route({path: "./suit/suit", query: {openId: user.openId}, clazzName: "SuitPage"})
    } else {
      wx.showToast({
        title: '请先授权登录^_^',
        icon: 'none',
      })
    }
  }
  toExchangeHistory = function() {
    var user = wx.getStorageSync('userInfo')
    if (user.openId) {
      this.$route({path: "./history/history", query: {openId: user.openId}, clazzName: "HistoryPage"})
    } else {
      wx.showToast({
        title: '请先授权登录^_^',
        icon: 'none',
      })
    }
  }
  toDaobidao = function() {
    this.$route({ path: "./about/about", query: {}, clazzName: "HistoryPage" })
  }
}

Page(new MyPage({clazzName: "MyPage"}))